//
//  Labs_String_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/2/18.
//

import UIKit

class Labs_String_DemoVC: UIViewController {
    
    var name: String?
    
    var nickName: NSString?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        stringRangeTest()
        rangeOfTest()
        specialTextsTest()
    }

    private func stringRangeTest() {
        let str: NSString = "你好，中国！你好，世界！"
        let range = str.range(of: "你好")
        print("\(#function) range is \(range)")
        /// range is {0, 2}
    }
    
    private func rangeOfTest() {
        var str: String = "你好，中国！你好，世界！"
        if let range = str.range(of: "你好") {
            print("\(#function) range is \(range)")
            str.removeSubrange(range)
        }
        print("str is \(str)")
    }
    
    private func specialTextsTest() {
        let lb = UILabel(frame: CGRect(x: 10, y: 150, width: 300, height: 50))
        view.addSubview(lb)
        lb.backgroundColor = .gray
        lb.numberOfLines = 0
        
        let attStr = NSMutableAttributedString()
        attStr.append("你好", font: UIFont.systemFont(ofSize: 15), textColor: .blue)
        attStr.append("，", font: UIFont.systemFont(ofSize: 12), textColor: .green)
        attStr.append("中国", font: UIFont.systemFont(ofSize: 30), textColor: .red)
        attStr.append("！", font: UIFont.systemFont(ofSize: 12), textColor: .green)
        attStr.append("你好", font: UIFont.boldSystemFont(ofSize: 20), textColor: .purple)
        attStr.append("，世界！", font: UIFont.systemFont(ofSize: 12), textColor: .green)
        lb.attributedText = attStr
        
        
        let lb2 = UILabel(frame: CGRect(x: 10, y: 250, width: 300, height: 50))
        view.addSubview(lb2)
        lb2.backgroundColor = .gray
        lb2.numberOfLines = 0
        
        let attStr2 = NSMutableAttributedString(string: "你好，中国！你好，世界！")
        attStr2.font = UIFont.systemFont(ofSize: 12)
        attStr2.textColor = .green
        attStr2.special(texts: ["你好", "你好", "中国"], fonts: [UIFont.systemFont(ofSize: 15), UIFont.boldSystemFont(ofSize: 20), UIFont.systemFont(ofSize: 30)], textColors: [.blue, .purple, .red])
        lb2.attributedText = attStr2
    }
    
}
